package org.example.cotroller.category;


import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Category;
import org.example.entity.PageBean;
import org.example.entity.Result;
import org.example.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     *商品类型表
     **/

    /**
     *根据id删除指定用户
     */

    @GetMapping("/{categoryId}")
    public Result delete(@PathVariable Integer categoryId){
        log.info("删除种类：{}",categoryId );
        if (categoryService.delete(categoryId))
            return Result.success();
        else return Result.error(categoryId +"不存在");
    }

    /**
     *查询种类
     */
    @GetMapping("")
    public Result pageList(@RequestParam(value = "categoryId",required = false, defaultValue = "") Integer categoryId,
                           @RequestParam(value = "categoryName",required = false, defaultValue = "") String categoryName,
                           @RequestParam(value = "category",required = false, defaultValue = "") Integer category,
                           @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "page",required = false, defaultValue = "1")Integer page){
        log.info("查询内容:{},{},{}",categoryId,categoryName,category);
        PageBean<Category> categoryPageBean = categoryService.categoryByPage(pageSize,page,categoryId,categoryName,category);
        return Result.success(categoryPageBean);
    }
    /**
     *查询种类
     */
    @GetMapping("/list")
    public Result categoryList(){
        log.info("查询内容:");
        List<String> categoryList = categoryService.categoryList();
        return Result.success(categoryList);
    }

    /**
     *新增种类
     */
    @PostMapping("")
    public Result add(@RequestBody Category category){
        log.info("新增种类：{}",category.toString());

        if (categoryService.add(category))
            return Result.success();
        else return Result.error("种类已存在");

    }

    /**
     *修改
     */
    @PostMapping("/{categoryId}")
    public Result update(@PathVariable Integer categoryId,@RequestBody Category category){
        log.info("修改种类{}信息:{}",categoryId,category);
        categoryService.update(category,categoryId);
        return Result.success();
    }
}
